﻿Shader "VertexDiffuse"
{
    Properties
    {
        _BaseColor("Color",Color) = (1,1,1,1)
        _BaseMap("Texture",2D) = "white"{}
        [KeywordEnum (Lambert,HalfLambert)] DIFFUSETYPE("Type", Float) = 0
    }
    SubShader
    {
        Tags 
        {
            "RenderPipeline"="UniversalRenderPipeline"
            "LightMode"="UniversalForward"
            "RenderType"="Opaque"
        }
        LOD 100

        HLSLINCLUDE
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
        
        #pragma multi_compile DIFFUSETYPE_LAMBERT DIFFUSETYPE_HALFLAMBERT 

        TEXTURE2D(_BaseMap);    SAMPLER(sampler_BaseMap);

        CBUFFER_START(UnityPerMaterial)
        float4 _BaseMap_ST;
        half4 _BaseColor;
        CBUFFER_END
        ENDHLSL
        
        pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            struct a2v
            {
                float4 positionOS : POSITION;
                float3 normalOS : NORMAL;
                float2 texcoord : TEXCOORD;
            };
            
            struct v2f
            {
                float4 positionCS : SV_POSITION;
                half2 texcoord : TEXCOORD0;
                half3 diffuse : COLOR;
            };
            
            v2f vert(a2v v)
            {
                v2f o;
                o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
                Light light = GetMainLight();
                half3 normalWS =TransformObjectToWorldNormal(v.normalOS.xyz);
                
                o.texcoord = TRANSFORM_TEX(v.texcoord,_BaseMap); 
                #ifdef DIFFUSETYPE_LAMBERT 
                    o.diffuse = _BaseColor.rgb * saturate(dot(normalWS, light.direction))* light.color;
                #elif DIFFUSETYPE_HALFLAMBERT
                    o.diffuse = _BaseColor.rgb * saturate(dot(normalWS, light.direction)* 0.5h+ 0.5h)* light.color;
                #endif
                return o;
            }

            half4 frag(v2f i):SV_Target
            {
                half4 albedo = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.texcoord);
                albedo.rgb *= i.diffuse;
                return float4(albedo.rgb, 1);
            }
            ENDHLSL
        }
    }
}